import {checkNetwork, checkFullScreen, checkIdcard, checkPhone, checkEmpty} from '../../static/js/util';
const app=getApp();
var fakedata=require("../../static/js/fakedata");
Page({ // eslint-disable-line
    data: {
        // value值
        formData: {
            phone: '',
            gender: 0,
            nickname:'',
            wechat:'',
            password:'',
            password2:''
        },
        phone:'',
        // 输入框与选择框的错误状态
        errorInfo: {
            phoneError: '',
            wechatError:'',
            nicknameError:''
        },
        // 输入框的弹窗
        noticInfo: {
            iptNoticInfo: {
                noticFlag: true,
                noticType: 'modal',
                noticModal: {
                    modalTitle: '标题',
                    modalContent: 'modal的一些个文案'
                }
            }
        },
        // 存放页面中的所有渲染的数据
        options: {
            // 单选框
            gender: [
                {name: '男'},
                {name: '女'}
            ]
        },
        // 判断是否为全面屏
        isFullScreen: checkFullScreen,
        // 加载loading，加载之前设置为true，加载完成后设置为false
        isPageLoading: false,
        // 错误页面状态
        pageResult: '',
        errorConfig: {
            noNetwork: {
                icon: 'wifi',
                title: '网络不给力，请稍后重试',
                showBtn: true
            },
            warning: {
                icon: 'warning',
                title: '服务器开小差，请稍后重试',
                showBtn: true
            },
            noData: {
                icon: 'content',
                title: '未查询到相关信息',
                desc: '请核实您的查询条件',
                showBtn: false
            }
        }
    },
    onLoad(e){
        this.setData({
            phone:e.phone
        });
    },
    check() {
        this.setData('isPageLoading', true);
        checkNetwork().then(res => {
            this.getDetail();
        })
        .catch(err => {
            this.setData({
                isPageLoading: false,
                pageResult: 'noNetwork'
            });
        });
    },
    iptKeyClean(e) {
        this.setData(`formData.${e.iptname}`, '');
    },
    iptKeyInput(e) {
        this.setData(`formData.${e.iptname}`, e.iptvalue);
    },
    iptblursjh(e) {
        this.setData('errorInfo.phoneError', checkPhone(e.iptvalue));
    },
    iptblurtext(e) {
        this.setData(`errorInfo.${e.iptname}Error`,checkEmpty(e.iptvalue,"请输入您的"+this.getChinese(e.iptname)))
    },
    submit() {
        let form=this.data.formData;
        if(form.nickname=='' || form.wechat=='' ||this.data.phone==''){
            swan.showModal({
                title: '请输入完整信息'
            });
            return false;
        }
        // if(this.data.formData.password!=this.data.formData.password2){
        //     swan.showModal({
        //         title: '密码输入不一致'
        //     });
        //     return false;
        // }
        console.log(this.data.formData);
        swan.request({
            url: fakedata.urls.adduser,
            header: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            method: 'POST',
            dataType: 'JSON',
            responseType: 'text',
            data: {
                nickname:this.data.formData.nickname,
                gender:this.data.formData.gender?'女':'男',
                wechat:this.data.formData.wechat,
                phone:this.data.phone,
                // password:this.data.formData.password
            },
            success: res => {
                if(res.statusCode==200){
                    swan.showModal({
                        title: '注册成功'
                    });
                    let data=JSON.parse(res.data);
                    let userinforeturn=data.data;
                    app.globalData.userinfo={
                        nickname:userinforeturn.nickname,
                        id:userinforeturn.id,
                        phone:userinforeturn.phone,
                        wechat:userinforeturn.wechat,
                        gender:userinforeturn.gender
                    };
                    app.globalData.islogin=true;
                    swan.switchTab({
                        url:'/pages/person/person'
                    });
                }else{
                    swan.showModal({
                        title:'服务器出错啦'
                    });
                }
            },
            fail: err => {
                swan.showModal({
                    title: '注册出错',
                    content: JSON.stringify(err)
                });
            }
        });
    },
    getChinese(e){
        if(e=='nickname') return "姓名"
        if(e=='wechat') return "微信号"
        if(e=='password') return "密码"
        if(e=='password2') return "确认密码"
    },
    showCurToast() {
        const {
            formData,
            errorInfo
        } = this.data;
        if (errorInfo.nicknameError.errFlag) {
            this.showToast("请输入您的昵称");
            return false;
        }
        if (errorInfo.phoneError.errFlag) {
            this.showToast('请输入正确的11位手机号');
            return false;
        }
        if (errorInfo.wechatError.errFlag) {
            this.showToast("请输入您的微信号");
            return false;
        }
        return true;
    },

    showToast(msg) {
        swan.showToast({
            title: msg,
            icon: 'none'
        });
    }
});
